package com.vhans.api.controller;

import cn.hutool.core.lang.Assert;
import com.vhans.core.web.controller.BaseController;
import com.vhans.core.web.model.Result;
import com.vhans.core.web.page.TableDataInfo;
import com.vhans.db.model.dto.ConditionDTO;
import com.vhans.db.model.dto.user.UserDTO;
import com.vhans.db.model.dto.user.UserPasswdDTO;
import com.vhans.db.model.vo.user.UserInfoVO;
import com.vhans.db.model.vo.user.UserVO;
import com.vhans.db.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 用户控制器
 *
 * @author vhans
 **/
@Api(tags = "用户模块")
@RestController
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    /**
     * 查看用户列表
     *
     * @param condition 条件
     * @return {@link UserVO} 用户列表
     */
    @ApiOperation(value = "查看用户列表")
    @GetMapping("/user/list")
    public TableDataInfo listUserBackVO(ConditionDTO condition) {
        startPage();
        List<UserVO> list = userService.listUser(condition);
        return getDataTable(list);
    }

    /**
     * 查看用户信息
     *
     * @return {@link UserVO} 用户信息
     */
    @ApiOperation(value = "获取用户信息")
    @GetMapping("/user/getInfo")
    public Result<UserInfoVO> getInfo() {
        UserInfoVO info = userService.getInfo();
        return Result.success(info);
    }


    /**
     * 修改用户
     *
     * @param user 用户信息
     * @return {@link Result<>}
     */
    @ApiOperation(value = "修改用户")
    @PutMapping("/user/update")
    public Result<?> updateUser(@Validated @RequestBody UserDTO user) {
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 修改用户密码
     *
     * @param passwd 密码
     * @return {@link Result<>}
     */
    @ApiOperation(value = "修改用户密码")
    @PutMapping("/user/passwd")
    public Result<?> updateUserPasswd(@Validated @RequestBody UserPasswdDTO passwd) {
        userService.updatePassword(passwd);
        return Result.success();
    }
}